/*
 * @FilePath: /ai_server/web_socket_log/config/config.h
 * @Author: arrow arrow8209@foxmail.com
 * @Date: 2023-01-10 13:52:07
 * @Description: 
 */
#pragma once
#include <cstdint>
#include <string>
#include <map>
#include <chrono>
#include <mutex>
#include "arrow/arrow.h"
#include "arrow/log.h"
#include "nlohmann/json.hpp"

namespace ArrowWebCodeView
{
class Config
{
    using MapStrToTp = std::map<std::string, std::chrono::steady_clock::time_point>;
public:
    Config() = default;
    virtual ~Config() = default;

    /**
     * @description: 读取所有配置信息
     * @param {char*} libgstmaxbase.so 绝对路径
     * @return {*}
     */
    bool Read(const char* szLocalFolder);

    const int32_t& Port() const;

    const std::string& DefalutFolder() const;

    const std::string& LastFolder() const;
    void LastFolder(const std::string& strFolder);

    const std::string& LastFile() const;
    void LastFile(const std::string& strFile);

    uint32_t TokenValidity();

    int32_t MaxFileSize() const;

    std::string Password() const;

    std::string GenerateToken();
    bool CheckToken(const std::string& strToken);
private:
    template <typename T>
    bool ParesJson(const nlohmann::json& j, T& t, const char* szKey)
    {
        try
        {
            t = j[szKey];
        }
        catch (const std::exception& ex)
        {
            ARROW_LOG_ERROR_FMT("read or parse %s fail.error msg %s", szKey, ex.what());
            return false;
        }
        return true;
    }

private:

    int32_t m_n32Version = 1;
    int32_t m_nPort = 60004;
    int32_t m_nTokenValidity = 3600*24; // token有效期 单位秒 [zhuyb 2024-06-14 17:02:18]
    int32_t m_nMaxFileSize = 2*1024*1024; // 最大文件大小 单位字节 [zhuyb 2024-06-14 17:02:18]
    std::string m_strPassword = "123456";
    std::string m_strDefalutFolder ="/root1";
    std::mutex m_mutexMapTokenToTp;
    MapStrToTp m_mapTokenToTp;
};

typedef Arrow::Pattern::Singleton<Config> AppConfig;
}